# -*- coding: utf-8 -*-
"""
Created on Sat Apr  3 19:25:41 2021

@author: Apple
"""


import os
import sys
from PyQt5 import QtCore,QtWidgets,QtGui
from PyQt5.QtWidgets import (QAbstractItemView,QMainWindow,QWidget,QTableWidget,QHBoxLayout,QMessageBox,QApplication,QTableWidgetItem,QHeaderView)
from main_window import Ui_MainWindow
from PyQt5.QtCore import Qt
import pandas as pd
import numpy as np
import math
import matplotlib.pyplot as plt
from PyQt5.QtGui import QPixmap
import mpl_toolkits.axisartist as axisartist
from matplotlib import patheffects

class MainWindow(QMainWindow):
    def __init__(self,parent=None):
        super(MainWindow,self).__init__(parent)
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        tableWidget11 = self.ui.tableWidget11
        tableWidget11.setRowCount(4)
        tableWidget11.setColumnCount(1)
        
        for i in range(4):
            newItem = QTableWidgetItem('')
            newItem.setTextAlignment(Qt.AlignCenter)
            tableWidget11.setItem(i,0,newItem)
        
        A = ['单位成本K','单位存储费C1','订购费C3','需求量R']
        tableWidget11.setVerticalHeaderLabels(A)
        tableWidget11.horizontalHeader().setVisible(False)  
        
        tableWidget21 = self.ui.tableWidget21
        tableWidget21.setRowCount(5)
        tableWidget21.setColumnCount(1)
        
        for i in range(5):
            newItem = QTableWidgetItem('')
            newItem.setTextAlignment(Qt.AlignCenter)
            tableWidget21.setItem(i,0,newItem)
        
        B = ['补货速率P','单位存储费C1','缺货费C2','生产准备费用C3','需求量R']
        tableWidget21.setVerticalHeaderLabels(B)
        tableWidget21.horizontalHeader().setVisible(False)  

        tableWidget31 = self.ui.tableWidget31
        tableWidget31.setRowCount(5)
        tableWidget31.setColumnCount(1)
        
        for i in range(5):
            newItem = QTableWidgetItem('')
            newItem.setTextAlignment(Qt.AlignCenter)
            tableWidget31.setItem(i,0,newItem)
        
        C = ['补货速率P','单位存储费C1','生产准备费用C3','需求量R','订货提前期t0']
        tableWidget31.setVerticalHeaderLabels(C)
        tableWidget31.horizontalHeader().setVisible(False)  

        tableWidget41 = self.ui.tableWidget41
        tableWidget41.setRowCount(4)
        tableWidget41.setColumnCount(1)
        
        for i in range(4):
            newItem = QTableWidgetItem('')
            newItem.setTextAlignment(Qt.AlignCenter)
            tableWidget41.setItem(i,0,newItem)
        
        D = ['单位存储费C1','缺货费C2','生产准备费用C3','需求量R']
        tableWidget41.setVerticalHeaderLabels(D)
        tableWidget41.horizontalHeader().setVisible(False) 
        
        self.ui.M1pic.setFrameShape(QtWidgets.QFrame.Box)
        self.ui.M2pic.setFrameShape(QtWidgets.QFrame.Box)
        self.ui.M3pic.setFrameShape(QtWidgets.QFrame.Box)
        self.ui.M4pic.setFrameShape(QtWidgets.QFrame.Box)
        
        self.ui.actionopenfile.triggered.connect(self.openfile)
        self.ui.actionversion.triggered.connect(self.showversion)

    def openfile(self):
        path = os.getcwd() 
        os.startfile(path)
    
    def showversion(self):
        QMessageBox.about(self, '关于', '当前版号为 V1.0 ')
    
    def M1save(self):
        tableWidget = self.ui.tableWidget11
        numrow = tableWidget.rowCount()
        numcol = tableWidget.columnCount()
        listall = []
        for i in range(numrow):
            listcol = []
            for j in range(numcol):
                data = tableWidget.item(i,j).text()
                if data =='':
                    QMessageBox.about(self, '警告', '请输入完整数据')
                    return
                else:
                    listcol.append(data)
            listall.append(listcol)
        print(listall)
        path = os.getcwd() + '\\1.csv'
        print(path)
        path = path.replace('\\','/')
        test = pd.DataFrame(data = listall)
        test.to_csv(path,index=0)
        QMessageBox.about(self, '完成', '保存成功')        
    
    def M1clear(self):
        tableWidget = self.ui.tableWidget11
        tableWidget.clear()
        tableWidget.setRowCount(4)
        tableWidget.setColumnCount(1)
        for i in range(4):
            newItem = QTableWidgetItem('')
            newItem.setTextAlignment(Qt.AlignCenter)
            tableWidget.setItem(i,0,newItem)
        B = ['单位成本K','单位存储费C1','订购费C3','需求量R']
        tableWidget.setVerticalHeaderLabels(B)
        tableWidget.horizontalHeader().setVisible(False)      
    
    def M1solve(self):
        data = pd.read_csv('1.csv')
        Data = data.iloc[:,:].values.tolist()
        K = Data[0][0]
        C1 = Data[1][0]
        C3 = Data[2][0]
        R = Data[3][0]
        t = math.sqrt(2*C3/(C1*R))
        Q = R*t
        C = math.sqrt(2*C1*C3*R)
        tableWidget = self.ui.tableWidget12
        tableWidget.setRowCount(3)
        tableWidget.setColumnCount(1)
        
        newItem = QTableWidgetItem(f'{format(t,".2f")}')
        newItem.setTextAlignment(Qt.AlignCenter)
        tableWidget.setItem(0,0,newItem)
        
        newItem = QTableWidgetItem(f'{format(Q,".2f")}')
        newItem.setTextAlignment(Qt.AlignCenter)
        tableWidget.setItem(1,0,newItem)
        
        newItem = QTableWidgetItem(f'{format(C,".2f")}')
        newItem.setTextAlignment(Qt.AlignCenter)
        tableWidget.setItem(2,0,newItem)
                
        B = ['订货周期t','经济订货批量Q','总费用C']
        tableWidget.setVerticalHeaderLabels(B)
        tableWidget.horizontalHeader().setVisible(False)  
        tableWidget.setEditTriggers(QAbstractItemView.NoEditTriggers)
    
    def M1clears(self):
        tableWidget = self.ui.tableWidget12
        tableWidget.clear()
        tableWidget.setRowCount(0)
        tableWidget.setColumnCount(0)

    
    def drawM1(self):
        data = pd.read_csv('1.csv')
        Data = data.iloc[:,:].values.tolist()
        K = Data[0][0]
        C1 = Data[1][0]
        C3 = Data[2][0]
        R = Data[3][0]
        t = math.sqrt(2*C3/(C1*R))
        Q = R*t
        
        x1 = np.linspace(0,t)
        y1 = Q+x1*-R
        
        x2 = np.linspace(t,2*t)
        y2 = -R*x2+2*Q
        
        fig = plt.figure(figsize=(8, 6))
        ax = axisartist.Subplot(fig, 111)
        fig.add_axes(ax)
        ax.axis[:].set_visible(False)
        
        ax.axis["x"] = ax.new_floating_axis(0,0)
        ax.axis["x"].set_axisline_style("->", size = 1.0)
        ax.axis["y"] = ax.new_floating_axis(1,0)
        ax.axis["y"].set_axisline_style("-|>", size = 1.0)
        ax.axis["x"].set_axis_direction("top")
        ax.axis["y"].set_axis_direction("right")
        
        plt.rcParams['font.sans-serif']=['SimHei']
        plt.rcParams['axes.unicode_minus']=False
        plt.plot(x1,y1,color='red',linestyle='-',linewidth=1,label='消耗阶段')
        plt.plot(x2,y2,color='green',linestyle='-',linewidth=1,label='补货阶段')

        plt.legend(loc='upper left') # 图例位置位于左上方
        plt.legend(loc='best')

        plt.title('存储状态图')
                
        plt.savefig('test.jpg',dpi=300)
        
        pic = QPixmap('test.jpg')
        self.ui.M1pic.setPixmap(pic)
        self.ui.M1pic.setScaledContents(True)
    
    def clearGraph(self):
        self.ui.M1pic.clear()

    def M2save(self):
        tableWidget = self.ui.tableWidget21
        numrow = tableWidget.rowCount()
        numcol = tableWidget.columnCount()
        listall = []
        for i in range(numrow):
            listcol = []
            for j in range(numcol):
                data = tableWidget.item(i,j).text()
                if data =='':
                    QMessageBox.about(self, '警告', '请输入完整数据')
                    return
                else:
                    listcol.append(data)
            listall.append(listcol)
        print(listall)
        path = os.getcwd() + '\\2.csv'
        print(path)
        path = path.replace('\\','/')
        test = pd.DataFrame(data = listall)
        test.to_csv(path,index=0)
        QMessageBox.about(self, '完成', '保存成功')        
    
    def M2clear(self):
        tableWidget = self.ui.tableWidget21
        tableWidget.clear()
        tableWidget.setRowCount(5)
        tableWidget.setColumnCount(1)
        for i in range(5):
            newItem = QTableWidgetItem('')
            newItem.setTextAlignment(Qt.AlignCenter)
            tableWidget.setItem(i,0,newItem)
        B = ['产量P','单位存储费C1','缺货费C2','生产准备费用C3','需求量R']
        tableWidget.setVerticalHeaderLabels(B)
        tableWidget.horizontalHeader().setVisible(False)      
    
    def M2solve(self):
        data = pd.read_csv('2.csv')
        Data = data.iloc[:,:].values.tolist()
        P= Data[0][0]
        C1 = Data[1][0]
        C2 = Data[2][0]
        C3 = Data[3][0]
        R = Data[4][0]
        t = math.sqrt(2*C3/(C1*R))*math.sqrt((C1+C2)/C2)*math.sqrt((P/(P-R)))
        Q = R*t
        t2 = C1*t/(C1+C2)
        t1 = (P-R)*t2/P
        t3 = R*t/P +(1-R/P)*t2
        A = R*(t-t3)
        B = R*t1
        C = 2*C3/t
        tableWidget = self.ui.tableWidget22
        tableWidget.setRowCount(8)
        tableWidget.setColumnCount(1)
        
        newItem = QTableWidgetItem(f'{format(t,".2f")}')
        newItem.setTextAlignment(Qt.AlignCenter)
        tableWidget.setItem(0,0,newItem)
        
        newItem = QTableWidgetItem(f'{format(Q,".2f")}')
        newItem.setTextAlignment(Qt.AlignCenter)
        tableWidget.setItem(1,0,newItem)
        
        newItem = QTableWidgetItem(f'{format(t2,".2f")}')
        newItem.setTextAlignment(Qt.AlignCenter)
        tableWidget.setItem(2,0,newItem)
        
        newItem = QTableWidgetItem(f'{format(t1,".2f")}')
        newItem.setTextAlignment(Qt.AlignCenter)
        tableWidget.setItem(3,0,newItem)
        
        newItem = QTableWidgetItem(f'{format(t3,".2f")}')
        newItem.setTextAlignment(Qt.AlignCenter)
        tableWidget.setItem(4,0,newItem)
                      
        newItem = QTableWidgetItem(f'{format(A,".2f")}')
        newItem.setTextAlignment(Qt.AlignCenter)
        tableWidget.setItem(5,0,newItem)
        
        newItem = QTableWidgetItem(f'{format(B,".2f")}')
        newItem.setTextAlignment(Qt.AlignCenter)
        tableWidget.setItem(6,0,newItem)
        
        newItem = QTableWidgetItem(f'{format(C,".2f")}')
        newItem.setTextAlignment(Qt.AlignCenter)
        tableWidget.setItem(7,0,newItem)
        
        B = ['存储周期t','经济生产批量Q','缺货补足时间t2','开始生产时间t1','结束生产时间t3','最大储存量A','最大缺货量B','平均总费用C']
        tableWidget.setVerticalHeaderLabels(B)
        tableWidget.horizontalHeader().setVisible(False)  
        tableWidget.setEditTriggers(QAbstractItemView.NoEditTriggers)
    
    def M2clears(self):
        tableWidget = self.ui.tableWidget22
        tableWidget.clear()
        tableWidget.setRowCount(0)
        tableWidget.setColumnCount(0)

    
    def drawM2(self):
        data = pd.read_csv('2.csv')
        Data = data.iloc[:,:].values.tolist()
        P= Data[0][0]
        C1 = Data[1][0]
        C2 = Data[2][0]
        C3 = Data[3][0]
        R = Data[4][0]
        t = math.sqrt(2*C3/(C1*R))*math.sqrt((C1+C2)/C2)*math.sqrt((P/(P-R)))
        t2 = C1*t/(C1+C2)
        t1 = (P-R)*t2/P
        t3 = R*t/P +(1-R/P)*t2
               
        x1 = np.linspace(0,t1)        
        y1 =-R*x1
        
        x2 = np.linspace(t1,t3)
        y2 = (P-R)*(x2-t2)
        
        x3 = np.linspace(t3,t)
        y3 = -R*(x3-t)
        
        fig = plt.figure(figsize=(8, 6))
        ax = axisartist.Subplot(fig, 111)
        fig.add_axes(ax)
        ax.axis[:].set_visible(False)
        
        ax.axis["x"] = ax.new_floating_axis(0,0)
        ax.axis["x"].set_axisline_style("->", size = 1.0)
        ax.axis["y"] = ax.new_floating_axis(1,0)
        ax.axis["y"].set_axisline_style("-|>", size = 1.0)
        ax.axis["x"].set_axis_direction("top")
        ax.axis["y"].set_axis_direction("right")
        
        plt.rcParams['font.sans-serif']=['SimHei']
        plt.rcParams['axes.unicode_minus']=False
        plt.plot(x1,y1,color='red',linestyle='-',linewidth=1,label='生产阶段')
        plt.plot(x2,y2,color='green',linestyle='-',linewidth=1,label='补货阶段')
        plt.plot(x3,y3,color='blue',linestyle='-',linewidth=1,label='消耗阶段')
        plt.legend(loc='upper left') # 图例位置位于左上方
        plt.legend(loc='best')

        plt.title('存储状态图')

                
        plt.savefig('test2.jpg',dpi=300)
        
        pic = QPixmap('test2.jpg')
        self.ui.M2pic.setPixmap(pic)
        self.ui.M2pic.setScaledContents(True)
    
    def clearGraph2(self):
        self.ui.M2pic.clear()
    
    def M3save(self):
        tableWidget = self.ui.tableWidget31
        numrow = tableWidget.rowCount()
        numcol = tableWidget.columnCount()
        listall = []
        for i in range(numrow):
            listcol = []
            for j in range(numcol):
                data = tableWidget.item(i,j).text()
                if data =='':
                    QMessageBox.about(self, '警告', '请输入完整数据')
                    return
                else:
                    listcol.append(data)
            listall.append(listcol)
        print(listall)
        path = os.getcwd() + '\\3.csv'
        print(path)
        path = path.replace('\\','/')
        test = pd.DataFrame(data = listall)
        test.to_csv(path,index=0)
        QMessageBox.about(self, '完成', '保存成功')        
    
    def M3clear(self):
        tableWidget = self.ui.tableWidget31
        tableWidget.clear()
        tableWidget.setRowCount(5)
        tableWidget.setColumnCount(1)
        for i in range(5):
            newItem = QTableWidgetItem('')
            newItem.setTextAlignment(Qt.AlignCenter)
            tableWidget.setItem(i,0,newItem)
        C = ['补货速率P','单位存储费C1','生产准备费用C3','需求量R','订货提前期t0']
        tableWidget.setVerticalHeaderLabels(C)
        tableWidget.horizontalHeader().setVisible(False)      
    
    def M3solve(self):
        data = pd.read_csv('3.csv')
        Data = data.iloc[:,:].values.tolist()
        P= Data[0][0]
        C1 = Data[1][0]
        C3 = Data[2][0]
        R = Data[3][0]
        t0 = Data[4][0]
        L = t0*R
        t = math.sqrt(2*C3*P/(C1*R*(P-R)))
        Q = R*t
        t3 = R*t/P
        A = R*(t-t3)
        C = 2*C3/t
        tableWidget = self.ui.tableWidget32
        tableWidget.setRowCount(6)
        tableWidget.setColumnCount(1)
        
        newItem = QTableWidgetItem(f'{format(L,".2f")}')
        newItem.setTextAlignment(Qt.AlignCenter)
        tableWidget.setItem(0,0,newItem)
        
        newItem = QTableWidgetItem(f'{format(t,".2f")}')
        newItem.setTextAlignment(Qt.AlignCenter)
        tableWidget.setItem(1,0,newItem)
        
        newItem = QTableWidgetItem(f'{format(Q,".2f")}')
        newItem.setTextAlignment(Qt.AlignCenter)
        tableWidget.setItem(2,0,newItem)
        
        newItem = QTableWidgetItem(f'{format(t3,".2f")}')
        newItem.setTextAlignment(Qt.AlignCenter)
        tableWidget.setItem(3,0,newItem)
        
        newItem = QTableWidgetItem(f'{format(A,".2f")}')
        newItem.setTextAlignment(Qt.AlignCenter)
        tableWidget.setItem(4,0,newItem)
                      
        newItem = QTableWidgetItem(f'{format(C,".2f")}')
        newItem.setTextAlignment(Qt.AlignCenter)
        tableWidget.setItem(5,0,newItem)
        
        C = ['订货点L','存储周期t','经济生产批量Q','结束生产时间t3','最大储存量A','平均总费用C']
        tableWidget.setVerticalHeaderLabels(C)
        tableWidget.horizontalHeader().setVisible(False)  
        tableWidget.setEditTriggers(QAbstractItemView.NoEditTriggers)
    
    def M3clears(self):
        tableWidget = self.ui.tableWidget32
        tableWidget.clear()
        tableWidget.setRowCount(0)
        tableWidget.setColumnCount(0)

    
    def drawM3(self):
        data = pd.read_csv('3.csv')
        Data = data.iloc[:,:].values.tolist()
        P= Data[0][0]
        C1 = Data[1][0]
        C3 = Data[2][0]
        R = Data[3][0]
        t0 = Data[4][0]
        L = t0*R
        t = math.sqrt(2*C3*P/(C1*R*(P-R)))
        Q = R*t
        t3 = R*t/P
        A = R*(t-t3)
        C = 2*C3/t
        
        x1 = np.linspace(0,t0)
        y1 = L-R*x1
        
        x2 = np.linspace(t0,t0+t3)
        y2 = (P-R)*(x2-t0)
        
        x3 = np.linspace(t3+t0,t+t0)
        y3 = -R*(x3-(t+t0))

        fig = plt.figure(figsize=(8, 6))
        ax = axisartist.Subplot(fig, 111)
        fig.add_axes(ax)
        ax.axis[:].set_visible(False)
        
        ax.axis["x"] = ax.new_floating_axis(0,0)
        ax.axis["x"].set_axisline_style("->", size = 1.0)
        ax.axis["y"] = ax.new_floating_axis(1,0)
        ax.axis["y"].set_axisline_style("-|>", size = 1.0)
        ax.axis["x"].set_axis_direction("top")
        ax.axis["y"].set_axis_direction("right")
        
        plt.rcParams['font.sans-serif']=['SimHei']
        plt.rcParams['axes.unicode_minus']=False
        plt.plot(x1,y1,color='red',linestyle='-',linewidth=1,label='订货阶段')
        plt.plot(x2,y2,color='green',linestyle='-',linewidth=1,label='补货阶段')
        plt.plot(x3,y3,color='blue',linestyle='-',linewidth=1,label='消耗阶段')
        plt.legend(loc='upper left') # 图例位置位于左上方
        plt.legend(loc='best')
        
        plt.xlim(0,t + 1.5*t0)
        plt.ylim(0,1.5*A)

        plt.title('存储状态图')
                
        plt.savefig('test3.jpg',dpi=300)
        
        pic = QPixmap('test3.jpg')
        self.ui.M3pic.setPixmap(pic)
        self.ui.M3pic.setScaledContents(True)
    
    def clearGraph3(self):
        self.ui.M3pic.clear()    

    def M4save(self):
        tableWidget = self.ui.tableWidget41
        numrow = tableWidget.rowCount()
        numcol = tableWidget.columnCount()
        listall = []
        for i in range(numrow):
            listcol = []
            for j in range(numcol):
                data = tableWidget.item(i,j).text()
                if data =='':
                    QMessageBox.about(self, '警告', '请输入完整数据')
                    return
                else:
                    listcol.append(data)
            listall.append(listcol)
        print(listall)
        path = os.getcwd() + '\\4.csv'
        print(path)
        path = path.replace('\\','/')
        test = pd.DataFrame(data = listall)
        test.to_csv(path,index=0)
        QMessageBox.about(self, '完成', '保存成功')        
    
    def M4clear(self):
        tableWidget = self.ui.tableWidget41
        tableWidget.clear()
        tableWidget.setRowCount(4)
        tableWidget.setColumnCount(1)
        for i in range(5):
            newItem = QTableWidgetItem('')
            newItem.setTextAlignment(Qt.AlignCenter)
            tableWidget.setItem(i,0,newItem)
        D = ['单位存储费C1','缺货费C2','生产准备费用C3','需求量R']
        tableWidget.setVerticalHeaderLabels(D)
        tableWidget.horizontalHeader().setVisible(False)      
    
    def M4solve(self):
        data = pd.read_csv('4.csv')
        Data = data.iloc[:,:].values.tolist()
        C1 = Data[0][0]
        C2 = Data[1][0]
        C3 = Data[2][0]
        R = Data[3][0]
        t = math.sqrt(2*C3*(C1+C2)/(C1*C2*R))
        Q = R*t
        tp = C1*t/(C1+C2)
        A = C2*R*t/(C1+C2)
        B = C1*R*t/(C1+C2)
        C = 2*C3/t
        tableWidget = self.ui.tableWidget42
        tableWidget.setRowCount(6)
        tableWidget.setColumnCount(1)
        
        newItem = QTableWidgetItem(f'{format(t,".2f")}')
        newItem.setTextAlignment(Qt.AlignCenter)
        tableWidget.setItem(0,0,newItem)
        
        newItem = QTableWidgetItem(f'{format(Q,".2f")}')
        newItem.setTextAlignment(Qt.AlignCenter)
        tableWidget.setItem(1,0,newItem)
        
        newItem = QTableWidgetItem(f'{format(tp,".2f")}')
        newItem.setTextAlignment(Qt.AlignCenter)
        tableWidget.setItem(2,0,newItem)
        
                      
        newItem = QTableWidgetItem(f'{format(A,".2f")}')
        newItem.setTextAlignment(Qt.AlignCenter)
        tableWidget.setItem(3,0,newItem)
        
        newItem = QTableWidgetItem(f'{format(B,".2f")}')
        newItem.setTextAlignment(Qt.AlignCenter)
        tableWidget.setItem(4,0,newItem)
        
        newItem = QTableWidgetItem(f'{format(C,".2f")}')
        newItem.setTextAlignment(Qt.AlignCenter)
        tableWidget.setItem(5,0,newItem)
        
        D = ['存储周期t','经济生产批量Q','生产时间tp','最大储存量A','最大缺货量B','平均总费用C']
        tableWidget.setVerticalHeaderLabels(D)
        tableWidget.horizontalHeader().setVisible(False)  
        tableWidget.setEditTriggers(QAbstractItemView.NoEditTriggers)
    
    def M4clears(self):
        tableWidget = self.ui.tableWidget42
        tableWidget.clear()
        tableWidget.setRowCount(0)
        tableWidget.setColumnCount(0)

    
    def drawM4(self):
        data = pd.read_csv('4.csv')
        Data = data.iloc[:,:].values.tolist()
        C1 = Data[0][0]
        C2 = Data[1][0]
        C3 = Data[2][0]
        R = Data[3][0]
        t = math.sqrt(2*C3*(C1+C2)/(C1*C2*R))
        Q = R*t
        tp = C1*t/(C1+C2)
        A = C2*R*t/(C1+C2)
        B = C1*R*t/(C1+C2)
        C = 2*C3/t
                
        x1 = np.linspace(0,tp)
        y1 = -R*x1
        
        x2 = np.linspace(tp,t+tp)
        y2 = -R*(x2-t)
        
        fig = plt.figure(figsize=(8, 6))
        ax = axisartist.Subplot(fig, 111)
        fig.add_axes(ax)
        ax.axis[:].set_visible(False)
        
        ax.axis["x"] = ax.new_floating_axis(0,0)
        ax.axis["x"].set_axisline_style("->", size = 1.0)
        ax.axis["y"] = ax.new_floating_axis(1,0)
        ax.axis["y"].set_axisline_style("-|>", size = 1.0)
        ax.axis["x"].set_axis_direction("top")
        ax.axis["y"].set_axis_direction("right")
        
        
        plt.rcParams['font.sans-serif']=['SimHei']
        plt.rcParams['axes.unicode_minus']=False
        plt.plot(x1,y1,color='red',linestyle='-',linewidth=1,label='生产阶段')
        plt.plot(x2,y2,color='green',linestyle='-',linewidth=1,label='消耗阶段')
        plt.legend(loc='upper left') # 图例位置位于左上方
        plt.legend(loc='best')
        
        plt.xlim(0,t + 1.5*tp)
        plt.ylim(-B,A)

        plt.title('存储状态图')               
        plt.savefig('test4.jpg',dpi=300)
        
        pic = QPixmap('test4.jpg')
        self.ui.M4pic.setPixmap(pic)
        self.ui.M4pic.setScaledContents(True)
    
    def clearGraph4(self):
        self.ui.M4pic.clear()     

if __name__=="__main__":
    app = QApplication(sys.argv)
    win = MainWindow()
    win.show()
    sys.exit(app.exec_())